using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;
using SlzrCrossGate.Core.Database;
using SlzrCrossGate.Core.Models;
using SlzrCrossGate.Core.Service;
using SlzrCrossGate.Core.Service.BusinessServices;
using SlzrCrossGate.WebAdmin.DTOs;


using SlzrCrossGate.WebAdmin.Services;
using System.Security.Claims;

namespace SlzrCrossGate.WebAdmin.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class TerminalsController : ControllerBase
    {
        private readonly TcpDbContext _dbContext;
        private readonly TerminalEventPublishService _terminalEventPublishService;
        private readonly UserService _userService;
        private readonly ILogger<TerminalsController> _logger;

        public TerminalsController(
            TcpDbContext dbContext,
            TerminalEventPublishService terminalEventPublishService,
            UserService userService,
            ILogger<TerminalsController> logger)
        {
            _dbContext = dbContext;
            _terminalEventPublishService = terminalEventPublishService;
            _userService = userService;
            _logger = logger;
        }

        // GET: api/Terminals
        [HttpGet]
        public async Task<ActionResult<PaginatedResult<TerminalDto>>> GetTerminals(
            [FromQuery] string? merchantId = null,
            [FromQuery] string? lineNo = null,
            [FromQuery] string? deviceNo = null,
            [FromQuery] string? machineId = null,
            [FromQuery] string? terminalType = null,
            [FromQuery] string? fileType = null,
            [FromQuery] string? fileVersion = null,
            [FromQuery] DeviceActiveStatus? activeStatus = null,
            [FromQuery] int page = 1,
            [FromQuery] int pageSize = 10)
        {
            // 获取当前用户的商户ID
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");

            // 如果不是系统管理员，只能查看自己商户的终端
            if (!isSystemAdmin && merchantId != null && merchantId != currentUserMerchantId)
            {
                return Forbid();
            }

            // 如果不是系统管理员且未指定商户ID，则使用当前用户的商户ID
            if (!isSystemAdmin && merchantId == null)
            {
                merchantId = currentUserMerchantId;
            }

            // 构建查询，包含商户信息
            var query = _dbContext.Terminals
                .Include(t => t.Status)
                .Where(t => !t.IsDeleted)
                .AsQueryable();



            // 应用筛选条件
            if (!string.IsNullOrEmpty(merchantId))
            {
                query = query.Where(t => t.MerchantID == merchantId);
            }

            if (!string.IsNullOrEmpty(lineNo))
            {
                query = query.Where(t => t.LineNO == lineNo);
            }

            if (!string.IsNullOrEmpty(deviceNo))
            {
                query = query.Where(t => t.DeviceNO == deviceNo);
            }

            if (!string.IsNullOrEmpty(machineId))
            {
                query = query.Where(t => t.MachineID == machineId);
            }

            if (!string.IsNullOrEmpty(terminalType))
            {
                query = query.Where(t => t.TerminalType == terminalType);
            }

            if (activeStatus.HasValue)
            {
                if (activeStatus.Value == DeviceActiveStatus.Active)
                {
                    query = query.Where(t => t.Status != null && t.Status.ActiveStatus == activeStatus.Value && t.Status.LastActiveTime > DateTime.Now.AddMinutes(-5));
                }
                else
                {
                    query = query.Where(t => t.Status == null || t.Status.ActiveStatus == DeviceActiveStatus.Inactive || t.Status.LastActiveTime < DateTime.Now.AddMinutes(-5));
                }
            }

            // 文件版本筛选需要特殊处理，因为是JSON字段
            if (!string.IsNullOrEmpty(fileType) && !string.IsNullOrEmpty(fileVersion))
            {
                // 这里需要根据数据库类型进行不同的处理
                // 对于MySQL，可以使用JSON_EXTRACT函数
                // 但由于EF Core的限制，这里我们先获取所有符合其他条件的终端，然后在内存中筛选
                var terminalsWithMerchantsForFilter = await query
                    .Join(_dbContext.Merchants,
                        t => t.MerchantID,
                        m => m.MerchantID,
                        (t, m) => new { Terminal = t, Merchant = m })
                    .Where(tm => !tm.Merchant.IsDelete)
                    .ToListAsync();

                var filteredTerminals = terminalsWithMerchantsForFilter.Where(tm =>
                    tm.Terminal.Status != null &&
                    tm.Terminal.Status.FileVersionMetadata.TryGetValue(fileType, out var version) &&
                    version.Current == fileVersion).ToList();

                var totalCount = filteredTerminals.Count;
                var pagedTerminals = filteredTerminals
                    .Skip((page - 1) * pageSize)
                    .Take(pageSize)
                    .ToList();

                var terminalDtos = pagedTerminals.Select(tm => new TerminalDto
                {
                    ID = tm.Terminal.ID,
                    MerchantID = tm.Terminal.MerchantID,
                    MerchantName = tm.Merchant.Name ?? "",
                    MachineID = tm.Terminal.MachineID,
                    DeviceNO = tm.Terminal.DeviceNO,
                    LineNO = tm.Terminal.LineNO,
                    TerminalType = tm.Terminal.TerminalType,
                    CreateTime = tm.Terminal.CreateTime,
                    Status = tm.Terminal.Status != null ? new TerminalStatusDto
                    {
                        ActiveStatus = tm.Terminal.Status.ActiveStatus,
                        LastActiveTime = tm.Terminal.Status.LastActiveTime,
                        ConnectionProtocol = tm.Terminal.Status.ConnectionProtocol ?? "",
                        EndPoint = tm.Terminal.Status.EndPoint ?? "",
                        FileVersionMetadata = tm.Terminal.Status.FileVersionMetadata,
                        PropertyMetadata = tm.Terminal.Status.PropertyMetadata
                    } : null,
                    TerminalID = tm.Terminal.ID,
                    TerminalTypeID = tm.Terminal.TerminalType,
                    IsActive = tm.Terminal.Status?.ActiveStatus == DeviceActiveStatus.Active,
                    CreatedTime = tm.Terminal.CreateTime,
                    UpdatedTime = tm.Terminal.CreateTime
                }).ToList();

                return new PaginatedResult<TerminalDto>
                {
                    Items = terminalDtos,
                    TotalCount = totalCount,
                    Page = page,
                    PageSize = pageSize
                };
            }

            // 使用JOIN查询获取终端和商户信息
            var joinQuery = query
                .Join(_dbContext.Merchants,
                    t => t.MerchantID,
                    m => m.MerchantID,
                    (t, m) => new { Terminal = t, Merchant = m })
                .Where(tm => !tm.Merchant.IsDelete);

            // 获取总记录数
            var count = await joinQuery.CountAsync();

            // 应用分页
            var terminalsWithMerchants = await joinQuery
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToListAsync();

            // 转换为DTO
            var terminalDtos2 = terminalsWithMerchants.Select(tm => new TerminalDto
            {
                ID = tm.Terminal.ID,
                MerchantID = tm.Terminal.MerchantID,
                MerchantName = tm.Merchant.Name ?? "",
                MachineID = tm.Terminal.MachineID,
                DeviceNO = tm.Terminal.DeviceNO,
                LineNO = tm.Terminal.LineNO,
                TerminalType = tm.Terminal.TerminalType,
                CreateTime = tm.Terminal.CreateTime,
                Status = tm.Terminal.Status != null ? new TerminalStatusDto
                {
                    ActiveStatus = tm.Terminal.Status.ActiveStatus,
                    LastActiveTime = tm.Terminal.Status.LastActiveTime,
                    ConnectionProtocol = tm.Terminal.Status.ConnectionProtocol ?? "",
                    EndPoint = tm.Terminal.Status.EndPoint ?? "",
                    FileVersionMetadata = tm.Terminal.Status.FileVersionMetadata,
                    PropertyMetadata = tm.Terminal.Status.PropertyMetadata
                } : null,

                // 兼容属性
                TerminalID = tm.Terminal.ID,
                TerminalTypeID = tm.Terminal.TerminalType,
                IsActive = tm.Terminal.Status?.ActiveStatus == DeviceActiveStatus.Active,
                CreatedTime = tm.Terminal.CreateTime,
                UpdatedTime = tm.Terminal.CreateTime
            }).ToList();

            return new PaginatedResult<TerminalDto>
            {
                Items = terminalDtos2,
                TotalCount = count,
                Page = page,
                PageSize = pageSize
            };
        }

        // GET: api/Terminals/5
        [HttpGet("{id}")]
        public async Task<ActionResult<TerminalDto>> GetTerminal(string id)
        {
            // 获取当前用户的商户ID
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");

            var terminalWithMerchant = await _dbContext.Terminals
                .Include(t => t.Status)
                .Join(_dbContext.Merchants,
                    t => t.MerchantID,
                    m => m.MerchantID,
                    (t, m) => new { Terminal = t, Merchant = m })
                .FirstOrDefaultAsync(tm => tm.Terminal.ID == id && !tm.Terminal.IsDeleted && !tm.Merchant.IsDelete);

            if (terminalWithMerchant == null)
            {
                return NotFound();
            }

            // 如果不是系统管理员，只能查看自己商户的终端
            if (!isSystemAdmin && terminalWithMerchant.Terminal.MerchantID != currentUserMerchantId)
            {
                return Forbid();
            }

            return new TerminalDto
            {
                ID = terminalWithMerchant.Terminal.ID,
                MerchantID = terminalWithMerchant.Terminal.MerchantID,
                MerchantName = terminalWithMerchant.Merchant.Name ?? "",
                MachineID = terminalWithMerchant.Terminal.MachineID,
                DeviceNO = terminalWithMerchant.Terminal.DeviceNO,
                LineNO = terminalWithMerchant.Terminal.LineNO,
                TerminalType = terminalWithMerchant.Terminal.TerminalType,
                CreateTime = terminalWithMerchant.Terminal.CreateTime,
                Status = terminalWithMerchant.Terminal.Status != null ? new TerminalStatusDto
                {
                    ActiveStatus = terminalWithMerchant.Terminal.Status.ActiveStatus,
                    LastActiveTime = terminalWithMerchant.Terminal.Status.LastActiveTime,
                    ConnectionProtocol = terminalWithMerchant.Terminal.Status.ConnectionProtocol ?? "",
                    EndPoint = terminalWithMerchant.Terminal.Status.EndPoint ?? "",
                    FileVersionMetadata = terminalWithMerchant.Terminal.Status.FileVersionMetadata,
                    PropertyMetadata = terminalWithMerchant.Terminal.Status.PropertyMetadata
                } : null,

                // 兼容属性
                TerminalID = terminalWithMerchant.Terminal.ID,
                TerminalTypeID = terminalWithMerchant.Terminal.TerminalType,
                IsActive = terminalWithMerchant.Terminal.Status?.ActiveStatus == DeviceActiveStatus.Active,
                CreatedTime = terminalWithMerchant.Terminal.CreateTime,
                UpdatedTime = terminalWithMerchant.Terminal.CreateTime
            };
        }

        // GET: api/Terminals/Stats
        [HttpGet("stats")]
        public async Task<ActionResult<TerminalStatsDto>> GetTerminalStats([FromQuery] string? merchantId = null)
        {
            // 获取当前用户的商户ID
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");

            // 如果不是系统管理员，只能查看自己商户的终端
            if (!isSystemAdmin && merchantId != null && merchantId != currentUserMerchantId)
            {
                return Forbid();
            }

            // 如果不是系统管理员且未指定商户ID，则使用当前用户的商户ID
            if (!isSystemAdmin && merchantId == null)
            {
                merchantId = currentUserMerchantId;
            }

            // 构建查询
            var query = _dbContext.Terminals
                .Include(t => t.Status)
                .Where(t => !t.IsDeleted)
                .AsQueryable();

            // 应用商户筛选
            if (!string.IsNullOrEmpty(merchantId))
            {
                query = query.Where(t => t.MerchantID == merchantId);
            }

            // 获取统计数据
            var totalCount = await query.CountAsync();
            var activeCount = await query.CountAsync(t => t.Status != null && t.Status.ActiveStatus == DeviceActiveStatus.Active && t.Status.LastActiveTime > DateTime.Now.AddMinutes(-5));
            var inactiveCount = await query.CountAsync(t => t.Status != null && (t.Status.ActiveStatus == DeviceActiveStatus.Inactive || t.Status.LastActiveTime < DateTime.Now.AddMinutes(-5)));

            return new TerminalStatsDto
            {
                TotalCount = totalCount,
                ActiveCount = activeCount,
                InactiveCount = inactiveCount
            };
        }

        // GET: api/Terminals/LineStats
        [HttpGet("linestats")]
        public async Task<ActionResult<PaginatedResult<LineStatsDetailDto>>> GetLineStats(
            [FromQuery] string? merchantId = null,
            [FromQuery] int page = 1,
            [FromQuery] int pageSize = 10)
        {
            // 获取当前用户的商户ID
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");

            // 如果不是系统管理员，只能查看自己商户的终端
            if (!isSystemAdmin && merchantId != null && merchantId != currentUserMerchantId)
            {
                return Forbid();
            }

            // 如果不是系统管理员且未指定商户ID，则使用当前用户的商户ID
            if (!isSystemAdmin && merchantId == null)
            {
                merchantId = currentUserMerchantId;
            }

            if (string.IsNullOrEmpty(merchantId))
            {
                return BadRequest("MerchantId is required");
            }

            try
            {
                // 使用原生SQL查询获取线路统计信息，性能更好
                var sql = @"
                    SELECT
                        t.LineNO,
                        COUNT(*) as Count,
                        COUNT(*) as TotalCount,
                        SUM(CASE
                            WHEN ts.ActiveStatus = 1 AND ts.LastActiveTime > DATE_SUB(NOW(), INTERVAL 5 MINUTE)
                            THEN 1 ELSE 0
                        END) as OnlineCount,
                        SUM(CASE
                            WHEN ts.ActiveStatus != 1 OR ts.LastActiveTime <= DATE_SUB(NOW(), INTERVAL 5 MINUTE) OR ts.ActiveStatus IS NULL
                            THEN 1 ELSE 0
                        END) as OfflineCount,
                        SUM(CASE
                            WHEN ts.ActiveStatus = 1 AND ts.LastActiveTime > DATE_SUB(NOW(), INTERVAL 5 MINUTE)
                            THEN 1 ELSE 0
                        END) as ActiveCount
                    FROM Terminals t
                    LEFT JOIN TerminalStatuses ts ON t.ID = ts.ID
                    WHERE t.MerchantID = {0} AND t.IsDeleted = 0
                    GROUP BY t.LineNO
                    ORDER BY t.LineNO
                    LIMIT {1} OFFSET {2}";

                var offset = (page - 1) * pageSize;

                // 执行查询
                var lineStats = await _dbContext.Database
                    .SqlQueryRaw<LineStatsDetailDto>(sql, merchantId, pageSize, offset)
                    .ToListAsync();

                // 使用EF Core查询获取总数，避免原生SQL的复杂性
                var totalCount = await _dbContext.Terminals
                    .Where(t => t.MerchantID == merchantId && !t.IsDeleted)
                    .Select(t => t.LineNO)
                    .Distinct()
                    .CountAsync();

                return new PaginatedResult<LineStatsDetailDto>
                {
                    Items = lineStats,
                    TotalCount = totalCount,
                    Page = page,
                    PageSize = pageSize
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error getting line stats for merchant {MerchantId}", merchantId);
                return StatusCode(500, "Internal server error");
            }
        }

        // GET: api/Terminals/5/Events
        [HttpGet("{id}/events")]
        public async Task<ActionResult<PaginatedResult<TerminalEventDto>>> GetTerminalEvents(
            string id,
            [FromQuery] TerminalEventType? eventType = null,
            [FromQuery] DateTime? startDate = null,
            [FromQuery] DateTime? endDate = null,
            [FromQuery] int page = 1,
            [FromQuery] int pageSize = 10)
        {
            // 获取当前用户的商户ID
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");

            // 检查终端是否存在
            var terminal = await _dbContext.Terminals.FirstOrDefaultAsync(t => t.ID == id && !t.IsDeleted);
            if (terminal == null)
            {
                return NotFound();
            }

            // 如果不是系统管理员，只能查看自己商户的终端
            if (!isSystemAdmin && terminal.MerchantID != currentUserMerchantId)
            {
                return Forbid();
            }

            // 构建查询
            var query = _dbContext.TerminalEvents
                .Where(e => e.TerminalID == id)
                .AsQueryable();

            // 应用筛选条件
            if (eventType.HasValue)
            {
                query = query.Where(e => e.EventType == eventType.Value);
            }

            if (startDate.HasValue)
            {
                query = query.Where(e => e.EventTime >= startDate.Value);
            }

            if (endDate.HasValue)
            {
                query = query.Where(e => e.EventTime <= endDate.Value);
            }

            // 获取总记录数
            var count = await query.CountAsync();

            // 应用分页和排序
            var events = await query
                .OrderByDescending(e => e.EventTime)
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToListAsync();

            // 转换为DTO
            var eventDtos = events.Select(e => new TerminalEventDto
            {
                ID = e.ID,
                MerchantID = e.MerchantID,
                TerminalID = e.TerminalID,
                EventTime = e.EventTime,
                EventName = e.EventName,
                EventType = e.EventType,
                Severity = e.Severity,
                Remark = e.Remark,
                Operator = e.Operator
            }).ToList();

            return new PaginatedResult<TerminalEventDto>
            {
                Items = eventDtos,
                TotalCount = count,
                Page = page,
                PageSize = pageSize
            };
        }

        // POST: api/Terminals/SendMessage
        [HttpPost("SendMessage")]
        public async Task<IActionResult> SendMessage([FromBody] SendMessageDto model)
        {
            // 获取当前用户的商户ID和用户名
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");
            var username = UserService.GetUserNameForOperator(User);

            // 验证终端列表
            var terminals = await _dbContext.Terminals
                .Where(t => model.TerminalIds.Contains(t.ID) && !t.IsDeleted)
                .ToListAsync();

            if (terminals.Count == 0)
            {
                return BadRequest("No valid terminals found");
            }

            // 如果不是系统管理员，只能向自己商户的终端发送消息
            if (!isSystemAdmin && terminals.Any(t => t.MerchantID != currentUserMerchantId))
            {
                return Forbid();
            }

            // 验证消息类型
            var msgType = await _dbContext.MsgTypes.FirstOrDefaultAsync(m => m.ID == model.MsgTypeCode);
            if (msgType == null)
            {
                return BadRequest("Invalid message type");
            }

            // 创建消息内容
            var msgContent = new MsgContent
            {
                MerchantID = terminals.First().MerchantID,
                MsgTypeID = model.MsgTypeCode,
                Content = model.Content,
                CreateTime = DateTime.Now,
                Operator = username
            };

            await _dbContext.MsgContents.AddAsync(msgContent);
            await _dbContext.SaveChangesAsync();

            // 为每个终端创建消息
            var msgBoxes = terminals.Select(t => new MsgBox
            {
                MerchantID = t.MerchantID,
                TerminalID = t.ID,
                MsgContentID = msgContent.ID,
                SendTime = DateTime.Now,
                Status = MessageStatus.Unread,
                ReadTime = null
            }).ToList();

            await _dbContext.MsgBoxes.AddRangeAsync(msgBoxes);
            await _dbContext.SaveChangesAsync();

            // 记录事件
            foreach (var terminal in terminals)
            {
                await _terminalEventPublishService.PublishTerminalEventAsync(new TerminalEventMessage
                {
                    MerchantID = terminal.MerchantID,
                    TerminalID = terminal.ID,
                    EventType = TerminalEventType.MessageSent,
                    Severity = EventSeverity.Info,
                    Remark = $"Message sent: Type={model.MsgTypeCode}, Content={model.Content}",
                    Operator = username
                });
            }

            return Ok(new { MessageId = msgContent.ID, TerminalCount = terminals.Count });
        }

        // POST: api/Terminals/PublishFile
        [HttpPost("PublishFile")]
        public async Task<IActionResult> PublishFile([FromBody] PublishFileDto model)
        {
            // 获取当前用户的商户ID和用户名
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");
            var username = UserService.GetUserNameForOperator(User);

            // 验证文件版本
            var fileVer = await _dbContext.FileVers.FirstOrDefaultAsync(f => f.ID == model.FileVerId && !f.IsDelete);
            if (fileVer == null)
            {
                return BadRequest("Invalid file version");
            }

            // 验证终端列表(文件版本的商户需要和终端的商户ID一致)
            var terminals = await _dbContext.Terminals
                .Where(t => model.TerminalIds.Contains(t.ID) && t.MerchantID == fileVer.MerchantID && !t.IsDeleted)
                .ToListAsync();

            if (terminals.Count == 0)
            {
                return BadRequest("No valid terminals found");
            }

            // 如果不是系统管理员，只能向自己商户的终端发布文件
            if (!isSystemAdmin && terminals.Any(t => t.MerchantID != currentUserMerchantId))
            {
                return Forbid();
            }

            // 检查文件版本是否已发布
            var existingFilePublish = await _dbContext.FilePublishs
                .AnyAsync(p => p.FileVerID == fileVer.ID && p.PublishType == PublishTypeOption.Terminal && model.TerminalIds.Contains(p.PublishTarget) && p.MerchantID == fileVer.MerchantID);
            if (existingFilePublish)
            {
                return Conflict("该发布已存在");
            }


            foreach (var terminal in model.TerminalIds)
            {
                // 创建文件发布记录
                var filePublish = new FilePublish
                {
                    MerchantID = fileVer.MerchantID,
                    FileTypeID = fileVer.FileTypeID,
                    FilePara = fileVer.FilePara,
                    FileFullType = fileVer.FileFullType,
                    Ver = fileVer.Ver,
                    FileSize = fileVer.FileSize,
                    Crc = fileVer.Crc,
                    FileVerID = fileVer.ID,
                    UploadFileID = fileVer.UploadFileID,
                    PublishType = PublishTypeOption.Terminal,
                    PublishTarget = terminal,
                    PublishTime = DateTime.Now,
                    Operator = username
                };

                await _dbContext.FilePublishs.AddAsync(filePublish);

                // 创建文件发布历史记录
                var filePublishHistory = new FilePublishHistory
                {
                    MerchantID = terminals.First().MerchantID,
                    FileTypeID = fileVer.FileTypeID,
                    FilePara = fileVer.FilePara,
                    FileFullType = fileVer.FileFullType,
                    Ver = fileVer.Ver,
                    FileSize = fileVer.FileSize,
                    Crc = fileVer.Crc,
                    FileVerID = fileVer.ID,
                    UploadFileID = fileVer.UploadFileID,
                    PublishType = PublishTypeOption.Terminal,
                    PublishTarget = terminal,
                    PublishTime = filePublish.PublishTime,
                    Operator = username
                };
                await _dbContext.FilePublishHistories.AddAsync(filePublishHistory);

            }
            await _dbContext.SaveChangesAsync();

            // 记录事件
            foreach (var terminal in terminals)
            {
                await _terminalEventPublishService.PublishTerminalEventAsync(new TerminalEventMessage
                {
                    MerchantID = terminal.MerchantID,
                    TerminalID = terminal.ID,
                    EventType = TerminalEventType.FilePublished,
                    Severity = EventSeverity.Info,
                    Remark = $"File published: Type={fileVer.FileFullType}, Version={fileVer.Ver}",
                    Operator = username
                });

            }

            return Ok(new {TerminalCount = terminals.Count });
        }

        // GET: api/Terminals/Export
        [HttpGet("export")]
        public async Task<IActionResult> ExportTerminals(
            [FromQuery] string? merchantId = null,
            [FromQuery] string? lineNo = null,
            [FromQuery] string? deviceNo = null,
            [FromQuery] string? machineId = null,
            [FromQuery] string? terminalType = null,
            [FromQuery] DeviceActiveStatus? activeStatus = null)
        {
            // 获取当前用户的商户ID
            var currentUserMerchantId = await _userService.GetUserMerchantIdAsync(User);
            var isSystemAdmin = User.IsInRole("SystemAdmin");

            // 如果不是系统管理员，只能查看自己商户的终端
            if (!isSystemAdmin && merchantId != null && merchantId != currentUserMerchantId)
            {
                return Forbid();
            }

            // 如果不是系统管理员且未指定商户ID，则使用当前用户的商户ID
            if (!isSystemAdmin && merchantId == null)
            {
                merchantId = currentUserMerchantId;
            }

            // 构建查询
            var query = _dbContext.Terminals
                .Include(t => t.Status)
                .Where(t => !t.IsDeleted)
                .AsQueryable();

            // 应用筛选条件
            if (!string.IsNullOrEmpty(merchantId))
            {
                query = query.Where(t => t.MerchantID == merchantId);
            }

            if (!string.IsNullOrEmpty(lineNo))
            {
                query = query.Where(t => t.LineNO == lineNo);
            }

            if (!string.IsNullOrEmpty(deviceNo))
            {
                query = query.Where(t => t.DeviceNO == deviceNo);
            }

            if (!string.IsNullOrEmpty(machineId))
            {
                query = query.Where(t => t.MachineID == machineId);
            }

            if (!string.IsNullOrEmpty(terminalType))
            {
                query = query.Where(t => t.TerminalType == terminalType);
            }

            if (activeStatus.HasValue)
            {
                query = query.Where(t => t.Status != null && t.Status.ActiveStatus == activeStatus.Value);
            }

            // 获取终端列表
            var terminals = await query.ToListAsync();

            // 转换为CSV格式
            var csv = new System.Text.StringBuilder();
            csv.AppendLine("ID,MerchantID,MachineID,DeviceNO,LineNO,TerminalType,CreateTime,ActiveStatus,LastActiveTime");

            foreach (var terminal in terminals)
            {
                csv.AppendLine($"{terminal.ID},{terminal.MerchantID},{terminal.MachineID},{terminal.DeviceNO},{terminal.LineNO},{terminal.TerminalType},{terminal.CreateTime},{terminal.Status?.ActiveStatus},{terminal.Status?.LastActiveTime}");
            }

            // 返回CSV文件
            return File(System.Text.Encoding.UTF8.GetBytes(csv.ToString()), "text/csv", "terminals.csv");
        }
    }
}
